package com.zyncro.test.multipart.httpclient;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import com.zyncro.test.multipart.Response;

public class HttpClientResponse implements Response {

	private final HttpResponse response;

	private final Map<String, String> headers = new HashMap<String, String>();

	private final int code;

	private final String body;

	private final InputStream stream;

	HttpClientResponse(HttpResponse response) {

		this.response = response;

		try {
			for (Header header : response.getAllHeaders()) {
				this.headers.put(header.getName(), header.getValue());
			}
			this.code = response.getStatusLine().getStatusCode();
			this.body = EntityUtils.toString(response.getEntity());
			this.stream = response.getEntity().getContent();

		} catch (Exception e) {
			throw new IllegalStateException("Problems parsing multipart response", e);
		}
	}

	@Override
	public int getCode() {
		return code;
	}

	@Override
	public String getBody() {
		return body;
	}

	@Override
	public InputStream getStream() {
		return stream;
	}

	@Override
	public String getHeader(String key) {
		return headers.get(key);
	}

	@Override
	public Map<String, String> getHeaders() {
		return headers;
	}

	public HttpResponse getHttpClientResponse() {
		return response;
	}
}